﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YunSpace.Application.SystemManage;
using YunSpace.Application.Topic;
using YunSpace.Core;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.Entity.Topic;
using YunSpace.Domain.ViewModel.SiteMgr;
using YunSpace.Domain.ViewModel.Topic;

namespace YunSpace.Web.Areas.Topic.Controllers
{
    public class ArticleController : ControllerBase
    {
        private readonly ArticleApp articleApp = new ArticleApp();
        private readonly ItemsDetailApp _itemsDetailApp = new ItemsDetailApp();
        private readonly ItemsApp _itemsApp = new ItemsApp();
        private readonly AreaApp _areaApp = new AreaApp();



        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="queryJson"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(string queryJson, Pagination pagination)
        {
            var request = queryJson.IsNullOrEmpty() 
                ? new GetGridJsonRequest() 
                : queryJson.ToObject<GetGridJsonRequest>();

            var data = new
            {
                rows = articleApp.GetArticles(request, pagination),
                pagination.total,
                pagination.page,
                pagination.records
            };

            return Content(data.ToJson());
        }


        /// <summary>
        /// 获取首页字典项数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetIndexItemsJson()
        {
            return Content((new
            {
                spaces = articleApp.GetSpaceMaps(),
                banners = _itemsDetailApp.GetItemList("Topic_Banner"),
                sources = _itemsDetailApp.GetItemList("Topic_Source"),
                auditStates = _itemsDetailApp.GetItemList("Topic_AuditState"),
                keywords = _itemsDetailApp.GetItemList("Topic_Keyword")
            }).ToJson());
        }

        /// <summary>
        /// 获取下拉框Json
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetIndexArticleTypesTreeSelectJson()
        {
            const string articleKey = "Topic_ArticleTypes_Article";
            const string miniSiteKey = "Topic_ArticleTypes_MiniSite";
            const string activityCaseKey = "Topic_ArticleTypes_ActivityCase";
            const string goodCaseKey = "Topic_ArticleTypes_GoodCase";
            const string specialKey = "Topic_ArticleTypes_Special";
            const string newsKey = "Topic_ArticleTypes_News";

            var treeList = new List<TreeSelectModel>();

            var items = _itemsApp.GetList().Where(x =>
                x.F_EnCode == articleKey || x.F_EnCode == miniSiteKey || x.F_EnCode == activityCaseKey ||
                x.F_EnCode == goodCaseKey || x.F_EnCode == specialKey || x.F_EnCode == newsKey).ToList();


            treeList.Add(new TreeSelectModel()
            {
                extend = "0",
                parentId = "0",
                text = "==请选资讯类型==",
                id = Guid.NewGuid().ToString(),
                data = new ItemsDetailEntity()
            });
            AddItemsToTreeList(treeList, items);

            var data = _itemsDetailApp.GetItemList(articleKey);
            AddItemsDetailsToTreeList(treeList, data);

            data = _itemsDetailApp.GetItemList(miniSiteKey);
            AddItemsDetailsToTreeList(treeList, data);

            data = _itemsDetailApp.GetItemList(activityCaseKey);
            AddItemsDetailsToTreeList(treeList, data);

            data = _itemsDetailApp.GetItemList(goodCaseKey);
            AddItemsDetailsToTreeList(treeList, data);

            data = _itemsDetailApp.GetItemList(specialKey);
            AddItemsDetailsToTreeList(treeList, data);

            data = _itemsDetailApp.GetItemList(newsKey);
            AddItemsDetailsToTreeList(treeList, data);


            return Content(treeList.TreeSelectJson());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SubmitForm(ArticleEntity articleEntity, string keyValue)
        {
            articleApp.SubmitForm(articleEntity, keyValue);
            return Success("成功");
        }

        /// <summary>
        /// 图片上传框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult ShowUploader()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetArticleFormJson(string keyValue)
        {
            return Content(articleApp.GetForm(keyValue).ToJson());
        }

        private static void AddItemsDetailsToTreeList(List<TreeSelectModel> trees, List<ItemsDetailActionModel> items)
        {
            foreach (var item in items)
            {
                var treeModel = new TreeSelectModel
                {
                    id = item.F_Id,
                    text = item.F_ItemName,
                    parentId = item.F_ItemId,
                    data = item,
                    extend = item.F_ItemCode
                };

                trees.Add(treeModel);
            }
        }

        private static void AddItemsToTreeList(List<TreeSelectModel> trees, List<ItemsEntity> items)
        {
            foreach (var item in items)
            {
                var treeModel = new TreeSelectModel
                {
                    id = item.F_Id,
                    text = item.F_FullName,
                    parentId = "0",
                    data = item,
                    extend = "0"
                };

                trees.Add(treeModel);
            }
        }
    }
}